class_name PropertyBuff
extends Buff



enum ModifiableProperty {
    PLAYER_HEALTH = 0,
    PLAYER_SPEED,
    PLAYER_SCALE,
    PLAYER_MAX_HEALTH,
    BULLET_DAMAGE = 10,
    BULLET_SPEED,
    BULLET_KNOCKBACK_FORCE,
    BULLET_RADIUS,
    GUN_FIRE_RATE,
    GUN_FIRE_BULLET_COUNT,
}

enum ModifiedWay {
    ADD,
    SUB,
}

enum ModifiedValueKind {
    NUM,
    PER,
}

enum ModifiedValueType {
    INT,
    FLOAT,
}


@export var modifiable_property: ModifiableProperty
@export var modified_way: ModifiedWay
@export var modified_value_kind: ModifiedValueKind
@export var modified_value_type: ModifiedValueType      ## 用于确定随机生成修改值的类型
@export var modified_value: float
@export var is_random_value: bool
@export var random_range: Vector2

var is_fixed: bool


func _fix_modified_value() -> void:
    if is_fixed: return
    is_fixed = true
    if not is_random_value: return
    match modified_value_type:
        ModifiedValueType.INT: modified_value = randi_range(int(random_range.x), int(random_range.y))
        ModifiedValueType.FLOAT: modified_value = randf_range(random_range.x, random_range.y)


func use(player: Player) -> void:
    var property: String
    match modifiable_property:
        ModifiableProperty.PLAYER_HEALTH: property = "health"
        ModifiableProperty.PLAYER_MAX_HEALTH: property = "max_health"
        ModifiableProperty.PLAYER_SPEED: property = "speed"
        ModifiableProperty.PLAYER_SCALE: property = "scale"
        ModifiableProperty.BULLET_DAMAGE: property = "bullet_damage"
        ModifiableProperty.BULLET_SPEED: property = "bullet_speed"
        ModifiableProperty.BULLET_KNOCKBACK_FORCE: property = "bullet_knockback_force"
        ModifiableProperty.BULLET_RADIUS: property = "bullet_radius"
        ModifiableProperty.GUN_FIRE_RATE: property = "fire_rate"
        ModifiableProperty.GUN_FIRE_BULLET_COUNT: property = "fire_bullet_count"
    
    var target = player if modifiable_property < 10 else player.gun
    var op_char = 1 if modified_way == ModifiedWay.ADD else -1
    
    _fix_modified_value()
    
    var new_value
    var old_value = target.get(property)
    match modified_value_kind:
        ModifiedValueKind.NUM:
            if typeof(old_value) == TYPE_INT or typeof(old_value) == TYPE_FLOAT:
                new_value = old_value + op_char * modified_value
            elif typeof(old_value) == TYPE_VECTOR2:
                new_value = old_value + op_char * Vector2(modified_value, modified_value)
        ModifiedValueKind.PER:
            new_value = old_value + op_char * modified_value / 100 * old_value
    
    target.set(property, new_value)
#    print("%s: %s" % [property, str(target.get(property))])


func get_text_dict() -> Dictionary:
    var color := convert_color()
    
    _fix_modified_value()
    
    return {
        "color": "#" + color.to_html(),
        "name": name,
        "desc": desc.format({"modified_value": modified_value})
    }


func update_modified_value(value: float) -> void:
    if not is_random_value:
        modified_value = value
    else:
        is_fixed = false
        _fix_modified_value()
    
